<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      https://leetcode-cn.com/problems/linked-list-cycle/
     */
    function hasCycle(head) {
      let fast = head;
      let slow = head;

      while (fast) { // 快指针指向真实节点
        if (fast.next === null) return false; // 如果下一个为null，说明没有环
        slow = slow.next; // 慢的走一步
        fast = fast.next.next; // 快的走两步
        if (slow === fast) return true; // 快慢指针相遇，有环
      }
      return false; // fastP指向null了，也始终不相遇
    }
  </script>
</body>
</html>